function HandlePageBranchHistory(gui, operator, now, options) {
  this._gui = gui;
  this._operator = operator;
  this._now = now;
  this._options = options;
  
  this._customerId = (options && options.id) ? options.id : null;
  this._orderTrackId = (options && options.track) ? options.track : [];
  this._createElements();
};

HandlePageBranchHistory.prototype._createElements = function() {
  this._gui.results.appendChild(DOMUtils.getLoadingImage());
  this._loadData();
};

HandlePageBranchHistory.prototype._loadData = function() {
  this._customer = null;
  this._produces = null;
  this._retrieveCustomer();
  this._retrieveProduces();
};

HandlePageBranchHistory.prototype._verifyData = function() {
  if (this._customer && this._produces) {
    this._updateElements();
  }
};

HandlePageBranchHistory.prototype._retrieveCustomer = function() {
  var _self = this;
  if (this._customerId) {
  var args = '&oid=' + this._customerId; 
  new RequestUtils()._mysql('v1customer', args, function(result, params) { _self._customer = (result.data.length == 1) ? result.data[0] : null;
                                                                          _self._verifyData.call(_self);
                                                                        }, {php:'customphp/mysqlread.php'});   
  
  } else {
    this._customer = v1customer.instance();
    this._customer.createdDate = this._now;
    this._customer.sales = this._operator;
    this._verifyData();
  }
};

HandlePageBranchHistory.prototype._retrieveProduces = function(pos) {
  var _self = this;
  if (this._customerId) {
  var args = '&oid=' + this._orderTrackId[0]; 
  new RequestUtils()._mysql('h1purchase', args, function(result, params) { _self._produces = (result.data.length == 1) ? result.data[0] : null;
                                                                          _self._verifyData.call(_self);
                                                                        }, {php:'customphp/mysqlread.php'});    
 
 
  } else {
    this._produces = [];
  }
};

HandlePageBranchHistory.prototype._updateElements = function() {
  var table, tr, td, order = DlmanageUtils.order(this._customer.orders, this._orderTrackId), _self = this;
  DOMUtils.removeChildElements(this._gui.results);
  table = document.createElement('table');
  table.style.width = '900px';
  table.cellPadding = 0;
  table.cellSpacing = 0;
  this._gui.results.appendChild(table);
  
  if (this._produces.length == 0) {
    tr = table.insertRow(-1);
    td = tr.insertCell(-1);
    td.style.textAlign = 'center';
    td.appendChild(document.createTextNode('N/A'));
    return;
  }
  /* 提取所有Produces */
 // var produces = MiscUtils.find(this._produces, [{ attrs: ['orderTrackId'], value: order.trackId }, { attrs: ['_deleted'], value: undefined }]);
 
 // op = MiscUtils.find(produces, [{ attrs: ['pi_model'], value: '{{$smarty.const.MODEL_NAME_PRECON|escape:javascript}}' }, { attrs: ['pi_model'], value: '{{$smarty.const.MODEL_NAME_MUSIC|escape:javascript}}' }, { attrs: ['pi_model'], value: '{{$smarty.const.MODEL_NAME_REQUIREMENT|escape:javascript}}' }], MiscUtils.disjunction);
  //wp = MiscUtils.find(produces, [{ attrs: ['pi_model'], value: '{{$smarty.const.MODEL_NAME_USER_PHOTOGRAPHER|escape:javascript}}' }, { attrs: ['pi_model'], value: '{{$smarty.const.MODEL_NAME_USER_VIDEOGRAPHER|escape:javascript}}' }, { attrs: ['pi_model'], value: '{{$smarty.const.MODEL_NAME_USER_MAKEUP|escape:javascript}}' }, { attrs: ['pi_model'], value: '{{$smarty.const.MODEL_NAME_USER_LIGHT|escape:javascript}}' }], MiscUtils.disjunction);
  
  /* 提取所有产品流程 ,并且分类到套餐产品流程和附加产品流程 */
  //gp = MiscUtils.find(produces, [{ attrs: ['pi_model'], value: '{{$smarty.const.MODEL_NAME_PRODUCT_VIDEO|escape:javascript}}' }, { attrs: ['pi_model'], value: '{{$smarty.const.MODEL_NAME_PRODUCT_VIDEODVD|escape:javascript}}' }, { attrs: ['pi_model'], value: '{{$smarty.const.MODEL_NAME_PRODUCT_4X6|escape:javascript}}' }, { attrs: ['pi_model'], value: '{{$smarty.const.MODEL_NAME_PRODUCT_RETOUCH_MAGAZINE|escape:javascript}}' }, { attrs: ['pi_model'], value: '{{$smarty.const.MODEL_NAME_PRODUCT_RETOUCH|escape:javascript}}' }, { attrs: ['pi_model'], value: '{{$smarty.const.MODEL_NAME_PRODUCT_COMMON|escape:javascript}}' }, { attrs: ['pi_model'], value: '{{$smarty.const.MODEL_NAME_ALBUM_DESIGN|escape:javascript}}' }], MiscUtils.disjunction);

  /* 提取所有产品 ,并且分类到套餐产品和附加产品 */
  purchases = MiscUtils.find(order.purchaseItems, [{ attrs: ['model'], value: '{{$smarty.const.MODEL_NAME_PRODUCT_VIDEO|escape:javascript}}' }, { attrs: ['model'], value: '{{$smarty.const.MODEL_NAME_PRODUCT_VIDEODVD|escape:javascript}}' }, { attrs: ['model'], value: '{{$smarty.const.MODEL_NAME_PRODUCT_4X6|escape:javascript}}' }, { attrs: ['model'], value: '{{$smarty.const.MODEL_NAME_PRODUCT_RETOUCH_MAGAZINE|escape:javascript}}' }, { attrs: ['model'], value: '{{$smarty.const.MODEL_NAME_PRODUCT_RETOUCH|escape:javascript}}' }, { attrs: ['model'], value: '{{$smarty.const.MODEL_NAME_PRODUCT_COMMON|escape:javascript}}' }, { attrs: ['model'], value: '{{$smarty.const.MODEL_NAME_ALBUM_DESIGN|escape:javascript}}' }, { attrs: ['model'], value: '' }], MiscUtils.disjunction);

 // for (var i = 0, il = produces.length; i < il; i++) {
 //   var produce = produces[i];
 //   tr = table.insertRow(-1);
//    td = tr.insertCell(-1);
//    td.style.textAlign = 'left';
//    td.style.padding = '10px 0 0 0';
//    new TableBranchHistory(td, produce, { user: this._operator, date: this._now, rest: purchases, callbackFunc: function(pos) { _self._retrieveProduces.call(_self, pos); }, produces: this._produces, purchases: order.purchaseItems, hidePrice: true, parent: order});
//  }
  
  MiscUtils.sort(purchases, ['name']);
  MiscUtils.sort(this._produces.purchaseItems, ['name']);
  //DlmanageUtils.compare(purchases, gp);
  
  //console.log(order);
  //console.log(this._produces.orders);
  
    tr = table.insertRow(-1);
    td = tr.insertCell(-1);
    td.style.textAlign = 'left';
    td.style.padding = '10px 0 0 0';
    new TableBranchHistory(td, order, { user: this._operator, date: this._now, rest: order, callbackFunc: function(pos) { _self._retrieveProduces.call(_self, pos); }, produces: this._produces.orders, purchases: order.purchaseItems, noLog: false, parent: order});
    
  
  for (var i = 0, il = purchases.length; i < il; i++) {
    var purchase = purchases[i];
    if (purchase._added) {
      continue;
    }
    
    var hpurchase = MiscUtils.find(this._produces.purchaseItems, [{ attrs: ['parent_id'], value: purchase.id }]);
    
    tr = table.insertRow(-1);
    td = tr.insertCell(-1);
    td.style.textAlign = 'left';
    td.style.padding = '10px 0 0 0';
    new TableBranchHistory(td, purchase, { user: this._operator, date: this._now, rest: purchases, callbackFunc: function(pos) { _self._retrieveProduces.call(_self, pos); }, produces: this._produces.purchaseItems, purchases: order.purchaseItems, noLog: true, parent: order});
  }
};
